<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Manage extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
// Load model m_data lên
        $this->load->model('m_loaihoa');
        $this->load->model('m_mausac');
        $this->load->model('m_hoa');
    }

    function index() {

        $this->load->view('test');
    }

    function image_upload() {

        $config['upload_path'] = './assets/imgs/Products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '1366';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('products_manage', $error);
        } else {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('products_manage', $data);
        }
    }

    function load_add_page() {

        $data['category'] = $this->m_loaihoa->getAllCategory();
        $data['color'] = $this->m_mausac->getAllColor();
        $this->load->view('addproduct', $data);
    }

    function forward() {

//        $this->load->model('user_model');
        if ($this->input->post('edit') != '') {
            $data['MaHoa'] = $this->input->post('MaHoa');
            $data['category'] = $this->m_loaihoa->getAllCategory();
            $data['color'] = $this->m_mausac->getAllColor();
            $this->load->view('updateproduct', $data);
        } else if ($this->input->post('delete') != '') {
            if ($this->delete_data())
                echo ($this->input->post('MaHoa') . ' deleted');
        }
//        $this->load->view('users_view');
//        redirect('users', 'refresh');
    }

    function update_data() {
        $MaHoa = $this->input->post('MaHoa');

        $config['upload_path'] = './assets/imgs/Products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '1366';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('products_manage', $error);
        } else {
            $upload_data = $this->upload->data();
            $HinhAnh = 'imgs/Products/' . $upload_data['file_name'];

            $data = array(
                'TenHoa' => $this->input->post('TenHoa'),
                'MaLoaiHoa' => $this->m_loaihoa->getMaLoaiHoa($this->input->post('LoaiHoa')),
                'MaMauSac' => $this->m_mausac->getMaMauSac($this->input->post('MauSac')),
                'HinhAnh' => $HinhAnh,
                'Soluong' => $this->input->post('SoLuong'),
                'DonGia' => $this->input->post('DonGia')
            );

            $this->m_hoa->updateProduct($MaHoa, $data);

            $this->load->view('info');
        }
    }

    function test() {
        echo ($this->m_loaihoa->getMaLoaiHoa('Chậu Hoa'));
    }

    function insert_data() {

        $config['upload_path'] = './assets/imgs/Products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '1366';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);
//        $MaLoaiHoa=$this->m_loaihoa->getMaLoaiHoa($this->input->post('LoaiHoa'));
//        $MaMauSac=$this->m_mausac->getMaMauSac($this->input->post('MauSac'));
//        echo $MaLoaiHoa;
//        echo $MaMauSac;
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('products_manage', $error);
        } else {
            $upload_data = $this->upload->data();
            $HinhAnh = 'imgs/Products/' . $upload_data['file_name'];
//$this->load->view('products_manage', $data);
            $data = array(
                'TenHoa' => $this->input->post('TenHoa'),
                'MaLoaiHoa' => $this->m_loaihoa->getMaLoaiHoa($this->input->post('LoaiHoa')),
                'MaMauSac' => $this->m_mausac->getMaMauSac($this->input->post('MauSac')),
                'HinhAnh' => $HinhAnh,
                'Soluong' => $this->input->post('SoLuong'),
                'DonGia' => $this->input->post('DonGia')
            );

            $this->m_hoa->insertProduct($data);

            $this->load->view('info');
        }
    }

    function delete_data() {
        $MaHoa = $this->input->post('MaHoa');
        $this->m_hoa->deleteProduct($MaHoa);
    }

}

?>